package com.ek.common.task;

import java.net.SocketAddress;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import com.ek.common.RoutingTable;
import com.ek.kademlia.DefaultBucket;
import com.ek.kademlia.protocol.Message;
import com.ek.service.mina.ComunicationService;
import com.ek.util.Utils;

public class AddContact extends FutureTask<Boolean> {

    public AddContact(final SocketAddress socketAddress, final Message message, final RoutingTable table) {
        super(new Callable<Boolean>() {

            public Boolean call() throws Exception {
                DefaultBucket bucket = new DefaultBucket();
                bucket.setAddress(Utils.getAddressOfSocketAddress(socketAddress));
                // TODO:now is setted but must be changed for different port
                bucket.setPort(ComunicationService.PORT);
                bucket.setNodeID(message.getNodeID());
                return table.addBucket(bucket);
            }
        });
    }
}
